package cn.wp.quartz.config;

import cn.wp.basic.constant.BusinessConstant;
import cn.wp.order.domain.ProductOrder;
import cn.wp.order.service.IProductOrderService;
import cn.wp.pay.domain.PayBill;
import cn.wp.pay.service.IPayBillService;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

public class MainJob implements Job {

    @Autowired
    private IPayBillService payBillService;

    @Autowired
    private IProductOrderService productOrderService;

    /**
     * 更改订单状态
     * @param jobExecutionContext
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // 获取定时任务的参数
        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        Map<String, Object> map = (Map<String, Object>) jobDataMap.get("params");
        // 获取订单号
        String orderSn = (String) map.get("orderSn");
        // 根据订单号查询支付单
        PayBill payBill = payBillService.loadByOrderSn(orderSn);
        // 判断如果状态为0改为-1
        if (payBill.getState() == 0){
            payBill.setState(-1);
            payBill.setUpdateTime(new Date());
        }
        payBillService.update(payBill);

        // 根据支付单的服务类型 和业务主键找到对应的订单
        switch (payBill.getBusinessType()){
            case BusinessConstant.PRODUCT_ORDER:// 服务订单
                ProductOrder productOrder = productOrderService.findOne(payBill.getBusinessKey());
                // 如果状态为0改为-1
                if (productOrder.getState()==0){
                    productOrder.setState(-1);
                    productOrderService.update(productOrder);
                }
                break;
            case  BusinessConstant.GOODS_ORDER:// 商品订单
                break;
        }
    }
}
